package com.googlecode.struts2scope.context.factory;

import java.util.Map;

import com.googlecode.struts2scope.context.Context;
import com.googlecode.struts2scope.scope.ScopeType;

public class ScopeContextFactory {

	private static ThreadLocal<Map<ScopeType, Context>> contextsMap = new ThreadLocal<Map<ScopeType, Context>>();
	
	@SuppressWarnings({ "unused", "unchecked" })
	public static Context getContext(ScopeType type) {
		synchronized (contextsMap) {
			Map<ScopeType, Context> contexts = contextsMap.get();
			if (contexts.containsKey(type)) {
				return contexts.get(type);
			}
			ContextFactory factory = type.getFactory();
			Context context = factory.buildContext();
			contexts.put(type, context);
			contextsMap.set(contexts);
			return context;
		}
	}
}
